package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.VerAddress;
import com.ruoyi.order.mapper.VerAddressMapper;
import com.ruoyi.order.service.IVerAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址Controller
 *
 * @author ruoyi
 * @date 2025-08-21
 */
@Slf4j
@RestController
@Tag(name = "地址接口", description = "提供地址操作")
@RequestMapping("/order/address")
public class VerAddressController extends BaseController {
    @Autowired
    private IVerAddressService verAddressService;
    @Autowired
    private VerAddressMapper verAddressMapper;

    /**
     * 查询地址列表
     */
    @PreAuthorize("@ss.hasPermi('order:address:list')")
    @Operation(summary = "查询地址列表", description = "传入地址查询对象，返回地址列表")
    @PostMapping("/list")
    public AjaxResult list(Long userId) {
        List<VerAddress> list = verAddressService.selectVerAddressList(userId);
        return success(list);
    }

    /**
     * 获取地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:address:query')")
    @Operation(summary = "获取地址详细信息", description = "传入地址 ID，返回地址详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(verAddressService.selectVerAddressById(id));
    }

    /**
     * 新增地址
     */
    @PreAuthorize("@ss.hasPermi('order:address:add')")
    @Log(title = "地址", businessType = BusinessType.INSERT)
    @Operation(summary = "新增地址", description = "传入地址对象，返回新增结果")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VerAddress verAddress) {
        // 添加调试日志
        log.info("Controller 接收到的参数: {}", verAddress);
        log.info("receiver 字段值: '{}'", verAddress.getReceiver());
        log.info("phone 字段值: '{}'", verAddress.getPhone());
        log.info("city 字段值: '{}'", verAddress.getCity());

        return toAjax(verAddressService.insertVerAddress(verAddress));
    }

    /**
     * 修改地址
     */
    @PreAuthorize("@ss.hasPermi('order:address:edit')")
    @Log(title = "地址", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改地址", description = "传入地址对象，返回修改结果")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody VerAddress verAddress) {
        return toAjax(verAddressService.updateVerAddress(verAddress));
    }

    /**
     * 删除地址
     */
    @PreAuthorize("@ss.hasPermi('order:address:remove')")
    @Log(title = "地址", businessType = BusinessType.DELETE)
    @Operation(summary = "删除地址", description = "传入地址 ID，返回删除结果")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(verAddressService.deleteVerAddressByIds(ids));
    }

    /**
     * 获取默认地址
     */
    @PreAuthorize("@ss.hasPermi('order:address:getDefault')")
    @Operation(summary = "获取默认地址", description = "传入用户 ID，返回默认地址")
    @GetMapping("/getDefault")
    public AjaxResult getDefault(@RequestParam Long userId) {
        return success(verAddressMapper.getDefault(userId));
    }
    /**
     * 设置默认地址
     */
    @PreAuthorize("@ss.hasPermi('order:address:setDefault')")
    @Operation(summary = "设置默认地址", description = "传入地址 ID，返回设置结果")
    @PostMapping("/setDefault")
    public AjaxResult setDefault(@RequestParam Long id, @RequestParam Long userId) {
        return success(verAddressService.updateDefault(id, userId));
    }
}
